const query = require('../config/connection');

//获取后台系统商品管理页面商品列表
async function getGoodsListService (ctx, params) {
  const start = (params.pageNo - 1) * params.pageSize;
  let sql = `SELECT * FROM petgoods limit ${start},${params.pageSize}`;

  try {
    let data = await query(sql);
    ctx.body = {
      "success": true,
      "data": data
    };
  } catch (error) {
    ctx.body = {
      "success": false,
      "message": error
    };
  }
};
//获取小程序商品列表
async function getWXGoodsListService (ctx, params) {
  const start = (params.pageNo - 1) * params.pageSize;
  let sql = `SELECT * FROM petgoods where DataStatus=2 limit ${start},${params.pageSize}`;
  try {
    let data = await query(sql);
    ctx.body = {
      "success": true,
      "data": data
    };
  } catch (error) {
    ctx.body = {
      "success": false,
      "message": error
    };
  }
};
//获取商品信息
async function getWXGoodsInfoService (ctx, params) {
  let sql = `SELECT * FROM petgoods where DataStatus=2 and GoodsNo=${params.goodsNo}`;
  try {
    let data = await query(sql);
    ctx.body = {
      "success": true,
      "data": data
    };
  } catch (error) {
    ctx.body = {
      "success": false,
      "message": error
    };
  }
};
//下架商品
async function downGoodsService (ctx, params) {
  let sql = `update petgoods set DataStatus=${params.dataStatus} where id=${params.id}`;
  try {
    let data = await query(sql);
    ctx.body = {
      "success": true,
      "data": "1"
    }
  } catch (error) {
    ctx.body = {
      "success": false,
      "message": error
    }
  }
}

//////////////////////////最新版接口 供h5和小程序调用////////////////
//获取商城商品列表 
async function getGoodsListService (ctx, params) {
  const start = (params.pageNo - 1) * params.pageSize;
  let sql = `SELECT * FROM petgoods where DataStatus=2 limit ${start},${params.pageSize} `;
  if (params.classify) {
    sql = `SELECT * FROM petgoods where DataStatus=2 and Classify= '${params.classify}'  limit ${start},${params.pageSize} `;
  }

  try {
    let data = await query(sql);
    ctx.body = {
      "success": true,
      "data": data
    };
  } catch (error) {

    ctx.body = {
      "success": false,
      "message": error
    };
  }
};

//获取商品详情
async function getGoodsDetailService (ctx, params) {
  let sql = `SELECT * FROM petgoods where GoodsNo=${params.goodsNo}`;
  try {
    let data = await query(sql);
    ctx.body = {
      "success": true,
      "data": data
    };
  } catch (error) {

    ctx.body = {
      "success": false,
      "message": error
    };
  }
};


module.exports = {
  getGoodsListService,
  downGoodsService,
  getWXGoodsListService,
  getWXGoodsInfoService,
  getGoodsDetailService
};
